<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    width="680px"
    :visible="visible"
    :append-to-body="true"
    :close-on-click-modal="true"
    custom-class="ele-dialog-form"
    title='冻结商家'
    @update:visible="updateVisible"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="82px">
      <el-row :gutter="15">
         <el-col v-bind="styleResponsive ? { sm: 12 } : { span: 12 }">
           <el-form-item label="选择商家:" prop="merchantIds">
             <role-select v-model="form.merchantIds" />
           </el-form-item>
        </el-col>
       </el-row>
    </el-form>
    <template v-slot:footer>
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script>
  import { emailReg, phoneReg } from 'ele-admin';
  import RoleSelect from './merchant-select.vue';
  import { addUser,pointRecharges, updateUser, checkExistence } from '@/api/merchantAccount';
  import {JSEncrypt} from "jsencrypt";

  export default {
    components: { RoleSelect },
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object
    },
    data() {
      const defaultForm = {
        id:'',
        merchantIds: [],
      };
      return {
        defaultForm,
        // 表单数据
        form: { ...defaultForm },
        // 表单验证规则
        rules: {
         merchantIds: [
            {
              required: true,
              message: '请输入商家名称',
              trigger: 'blur'
            }
          ],
         merchantName: [
           {
             required: true,
             message: '请输入商家名称',
             trigger: 'blur'
           }
         ],
         merchantCode: [
           {
             required: true,
             message: '请输入商家账号',
             trigger: 'blur'
           }
         ],
         prescription: [
           {
             required: true,
             message: '请选择有效期',
             trigger: 'blur'
           }
         ],
         counts: [
           {
             required: true,
             message: '请输入点数',
             trigger: 'blur'
           }
         ],
         password: [
           {
             required: true,
             pattern: /^[\S]{5,18}$/,
             message: '密码必须为5-18位非空白字符',
             trigger: 'blur'
           }
         ],
        },
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false
      };
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    methods: {
      uploadPrice(e){
        console.log('e',this.from)
        // return
        this.confirm()
      },
      /* 保存编辑 */
      save() {
        this.$refs.form.validate((valid) => {
          if (!valid) {
            return false;
          }
          this.loading = true;
          const data = {
            ...this.form,
            id: this.form.merchantIds[0]
          };
          console.log('================11', data)
          pointRecharges(data)
            .then((msg) => {
              this.loading = false;
              this.$message.success(msg);
              this.form.counts=''
              this.updateVisible(false);
              this.$emit('done');
            })
            .catch((e) => {
              this.loading = false;
              this.$message.error(e.message);
            });
        });
      },
      /* 更新visible */
      updateVisible(value) {
        this.$emit('update:visible', value);
      },
      loadClick(){
        /* this.$nextTick(() => {
          this.$refs['edits'].show()
        }) */
        setTimeout(()=>{

          this.$refs['managementEdit'].show()
        },200)
      }
    },
    watch: {
      visible(visible) {
        if (visible) {
          if (this.data) {
            console.log("this.data",this.data)
            this.$util.assignObject(this.form, {
              ...this.data,
              merchantIds: this.data.id,
              password: null,
              sex: this.data.sex + ''
            });
            this.isUpdate = true;
            this.loadClick()
          } else {
            this.isUpdate = false;
          }
        } else {
          this.$refs.form.clearValidate();
          this.form = { ...this.defaultForm };
        }
        // 清空‘充值进入’输入框默认值
        this.form.counts=''
        console.log("this.form",this.form)

      }
    }
  };
</script>
